// miniprogram/pages/joke/joke.js
const app = getApp()
const jinrishici = require('../../utils/jinrishici.js')
const request = require('../../utils/request.js');
let util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tips: '请稍后',
    show: true,
    animated: true,
    title: "笑一笑",
    style: app.globalData.highlightStyle,
    refreshing: false,
    list: [],
    current: 1,
    noMore: false,
    /* 为true表示加载中 */
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.queryJokeList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  queryJokeList: function(){
    this.setData({
      show: true
    });
    var urlContent = app.globalData.jokeUrl + '/api/joke/content/latest';
    var params = {current: this.data.current, size: 10};

    var token = app.globalData.token;
    var that = this;
    //@todo 文章内容网络请求API数据
    wx.request({
      url: urlContent,
      method: "GET",
      data: params,
      header: { 
          'joke_token': token
      },
      success: function (res) {
          console.log(res)
          if(res.data && res.data.code == 200){

            if (res.data.data.length < 1) {
              that.setData({
                show: false,
                noMore: true
              })
              return;
            }

            that.setData({
              list: that.data.list.concat(res.data.data),
              current: that.data.current + 1,
              refreshing: false,
              show: false
            });
          }
      },
      fail: function (res) {
        console.log(res)
      }
  })
  },

  refreshNew:function(){
    if(!this.data.noMore){
      console.log('滑动刷新...')
      this.queryJokeList()
    }
  }
})